local Widget = require "widgets/widget"
local Button = require "widgets/button"
local UIAnim = require "widgets/uianim"

local AnimButton = Class(Button, function(self, animname, cmd)
    Button._ctor(self, "AnimButton_NPC_Card")
    self.anim = self:AddChild(UIAnim())
    self.anim:MoveToBack()
    self.anim:GetAnimState():SetBuild(animname)
    self.anim:GetAnimState():SetBank(animname)

    self.anim:GetAnimState():PlayAnimation("idle",true)
    -- self.anim:GetAnimState():SetRayTestOnBB(true); ----- 这一段导致异常偏移

	-- self.focus_flag = nil
	
	local tempInst = CreateEntity()
	self.anim:GetAnimState():SetMultColour(255,255,255,0)
	-- self:Disable()

	tempInst.__display_task_num = 0
	tempInst.__display_task = tempInst:DoPeriodicTask(0.1,function()
		tempInst.__display_task_num = tempInst.__display_task_num + 0.2
		self.anim:GetAnimState():SetMultColour(255,255,255,tempInst.__display_task_num)
		if tempInst.__display_task_num > 1 and tempInst.__display_task then
			---------------------------------------------------------------
			self.anim:GetAnimState():SetMultColour(255,255,255,1)
			-- self:Enable()			
			if self.focus_flag then
				self:SetFocus()
			end
			---------------------------------------------------------------
			tempInst.__display_task:Cancel()
			tempInst:Remove()
		end
	end)
	
	self.scale_num = 1
	self.scale_num_focus = 1.2
	if cmd then
		if cmd.scale_num then
			self.scale_num = cmd.scale_num
		end
		if cmd.scale_num_focus then
			self.scale_num_focus = cmd.scale_num_focus
		end

		if cmd.index then
			self.index = cmd.index
		end
	end
	self:SetScale(self.scale_num,self.scale_num)


end)

function AnimButton:OnGainFocus()
	AnimButton._base.OnGainFocus(self)

	self.parent._focus_index = self.index
	self.parent._focus_button = self
	-- print("button index",self.parent._focus_index)

    if self:IsEnabled() then
		-- self.anim:GetAnimState():PlayAnimation(self.animstates and self.animstates.over or "over")
		self:SetScale(self.scale_num_focus,self.scale_num_focus)
	end
end

function AnimButton:OnLoseFocus()
	AnimButton._base.OnLoseFocus(self)

	if self:IsEnabled() then
		-- self.anim:GetAnimState():PlayAnimation(self.animstates and self.animstates.idle or "idle")
		self:SetScale(self.scale_num,self.scale_num)
    end
end


function AnimButton:Enable()
	AnimButton._base.Enable(self)
	-- self.anim:GetAnimState():PlayAnimation(self.animstates and self.animstates.idle or "idle")
	--self.text:SetColour(1,1,1,1)
end

function AnimButton:Disable()
	AnimButton._base.Disable(self)
	-- self.anim:GetAnimState():PlayAnimation(self.animstates and self.animstates.disabled or "disabled")
	--self.text:SetColour(.7,.7,.7,1)
end

return AnimButton